[ Home | Prev | Next ]

Chapter 10: continued


Agents

An agent is a UserTalk script that runs as a background process at specified intervals. Frontier can have, in theory, any number of agent scripts running at the same time; other activities proceed as normal. Agents run as long as Frontier is running, whether or not Frontier is the frontmost application.

Agents are stored in the system.agents table. Be default, agents run once per second. Many agents don't need to run nearly so often; Frontier provides a verb that will put an agent to "sleep" for a specified number of seconds:

clock.sleepFor (60)

When Frontier encounters this line in an agent script, it sets up a 60-second timer. When the timer expires, the script executes again. This verb is only allowed in agent scripts. If you run an agent script directly, Frontier will generate an error.

Let's take a look at an agent script. Jump to system.agents.MinutesSinceShip; you should see something like the following.


Sample Agent Script

Agents scripts have standard script editing windows. Click on the "Run" button in this window. The script will execute once and then you'll see a Frontier Error Info window:


Error Generated by Running an Agent Script from Its Window

Even though the script is stored in the proper table and running in the background as an agent, when you run it directly, that "instance" of the script is not running as an agent. Close the error window and the script's editing window. Now go to the Main Window and select "MinutesSinceShip" from the popup menu (the down arrow on the left side). The agent's message will appear in the window. If you are patient enough to wait, you will see that the Main Window is indeed updated every minute.


Main Window With minutesSinceShip Agent Running

Although you can have multiple agents running, only one can display messages in the Main Window, the one selected in the popup. The other agents still run but their messages quietly disappear.

Example: Appointment Reminder

Let's build a useful agent script. This simple script will "wake up" every minute and check your people table at a location called nextAppointment. If the time it finds there matches the present time, it will sound the Macintosh speaker and display a dialog reminding you of the appointment. First, create a new item in your people table, e.g. people.ME.nextAppointment. To save you the trouble of entering the current date and time in the proper format, here's a cool trick. Tab to the value column for this item and type:

clock.now ()

Hit the return key or enter key or just click the mouse in another cell. Frontier is smart enough to recognize that clock.now is a verb, so it runs the verb and places the result in the cell.

To run as an agent, a script has to reside in the table system.agents, so open that table and create a new script named "myReminder." Here's the code for this script:

The first line compares the date-and-time value stored at the nextAppointment spot in the user's table to the present date-and-time. If the two values are equal, Frontier sounds the speaker and displays a dialog informing you that it's time for your next appointment. If the two times and dates don't match, the agent goes to sleep for 60 seconds and then checks again. One technical note: remember that square brackets around an expression, user.initials in this case, will evaluate the expression and use the result as the name of a single table in the path. This approach is much more general than typing your intials, making it much easier to share the agent with someone else.

Does the agent work? Click "Compile" to activate it. If you have the normal StatusMessage agent selected in the Main Window's popup, it will show you the current time. Switch to your people table and edit the value of nextAppointment so it's a few seconds ahead. Be sure to hit enter or return or click outside the cell to "commit" the change to the Object Database; otherwise Frontier still remembers the previous value. If everything is set up correctly, you should see the dialog when the actual time reaches the appointment time.

You could use this agent as a starting point for a script that would check multiple appointments in a table in the Object Database, perhaps to remind you when it is time to leave for a meeting or to eat your lunch.

Agents have all kinds of potential uses, such as backing up your hard disk to a network volume late at night, automatically saving your root file every hour, or watching a folder for new files to process. Use your imagination; you'll probably come up many interesting applications.

Sharing Scripts

If you write a UserTalk script that is especially useful or just plain cool, please consider sharing it! Frontier has an active "electronic community," dating back to pre-release versions of the product in 1991. You will find interesting and helpful script writers, ranging from beginner to expert, on the Internet, CompuServe and America On-Line. Please join us!

Contents Page | Previous Section | Next -- Glossary
HTML formatting by Steven Noreyko January 1996, User Guide revised by UserLand June 1996